package com.demo.zhihu.web;

import javax.inject.Inject;
import javax.inject.Singleton;

import com.demo.zhihu.model.Answer;
import com.demo.zhihu.model.Comment;
import com.demo.zhihu.model.Question;
import com.demo.zhihu.model.User;
import com.demo.zhihu.service.QuestionService;

import cn.techarts.dragonfly.web.WebContext;
import cn.techarts.dragonfly.web.rest.Get;
import cn.techarts.dragonfly.web.rest.Post;
import cn.techarts.dragonfly.web.rest.Restful;

@Singleton
@Restful
public class QuestionWebService {

    @Inject
    private QuestionService service;

    @Post("/question/{asker}")
    public Question ask(WebContext arg) {
        var asker = arg.intAt(0);
        var title = arg.get("title");
        var summary = arg.get("summary");
        return service.ask(asker, title, summary);
    }
    
    @Post("/answer/{question}/{user}")
    public Answer answer(WebContext arg) {
        var question = arg.intAt(0);
        var user = arg.intAt(1);
        var content = arg.get("content");
//        var content = "222222";
        var result = service.answer(question, user, content);
        if(content==null||content.isEmpty()){
            arg.error(-1,"未接收到content参数");
            return null;
        }
        if (result == null) {
            arg.error(-11, "The user has answered the question");
        }
        return result;
    }
        @Post("/answer/{id}/comment/{commenter}")
        public Comment comment(WebContext arg) {
            var answer = arg.intAt(0);
            var commenter = arg.intAt(1);
            var content = arg.get("content");
            return service.comment(answer, commenter, content);
        }

        @Post("/comment/{id}/reply/{commenter}")
        public Comment reply(WebContext arg) {
            var comment = arg.intAt(0);
            var commenter = arg.intAt(1);
            var content = arg.get("content");
            if(content==null||content.isEmpty()){
                arg.error(-1,"未接收到content参数");
                return null;
            }
            return service.reply(comment, commenter, content);
        }

     
        @Post("/user/{id}/like/answer/{id}")
        public boolean likeAnswer(WebContext arg) {
            var user = arg.intAt(0);
            var answer = arg.intAt(1);
            return service.likeSomeone(Answer.ME, answer, user);
        }

        @Post("/like/comment/{id}/user/{id}")
        public boolean likeComment(WebContext arg) {
            var comment = arg.intAt(0);
            var user = arg.intAt(1);
            return service.likeSomeone(Comment.ME, comment, user);
        }

      
}
